package com.koocloud.electroplatemanage.controller;

import com.koocloud.electroplatemanage.common.pojo.ResponseTemplate;
import com.koocloud.electroplatemanage.pojo.IncomeBasicInfoVo;
import com.koocloud.electroplatemanage.service.IncomeBasicInfoService;
import com.koocloud.electroplatemanage.utils.Base64Utils;
import com.koocloud.electroplatemanage.utils.DeleteFileUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

/**
 * @program: Information_platform
 * @description: 来件基本信息控制层
 * @author: xxc
 * @create: 2020-11-13 14:54
 */
@RestController
@RequestMapping("/incomebasicinfo")
public class IncomeBasicInfoController {
    static Logger log = Logger.getLogger("incomebasicinfo");

    @Autowired
    IncomeBasicInfoService incomeBasicInfoService;

    /**
     *@Description: 通过工单号查询一条来件基本信息
     *@Param: [workOrderNumber]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/11/13
     */
    @RequestMapping(value = "/selectByWorkOrderNumber", method = RequestMethod.POST, name = "通过工单号查询一条来件基本信息")
    public ResponseTemplate selectByWorkOrderNumber(String workOrderNumber) {
        //通过工单号查询来件基本信息
        return incomeBasicInfoService.selectByWorkOrderNumber(workOrderNumber);
    }

    /**
     *@Description: 对来件基础信息进行全表查询
     *@Param: [page, limit, incomeBasicInfoVo]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/11/13
     */
    @RequestMapping(value = "/selectAllData", method = RequestMethod.POST, name = "对来件基础信息进行全表查询")
    public ResponseTemplate selectAllData(String page, String limit, IncomeBasicInfoVo incomeBasicInfoVo) {
        //分页参数为空则赋值0
        if (!StringUtils.isNotEmpty(page)) {
            page = "0";
        }
        if (!StringUtils.isNotEmpty(limit)) {
            limit = "0";
        }
        //查询所有记录 分页
        return incomeBasicInfoService.selectAllData(page,limit,incomeBasicInfoVo);
    }

    /**
     *@Description: 新增一条来件基础信息数据
     *@Param: [incomeBasicInfoVo]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/11/13
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST, name = "新增一条数据")
    public ResponseTemplate insert(HttpServletRequest request, @RequestBody IncomeBasicInfoVo incomeBasicInfoVo) {
/*        //获取上传时的文件名称
        String fileName = files.getOriginalFilename();
        String file = Base64Utils.GetImageStr(fileName);
        incomeBasicInfoVo.setImagePath(file);*/
        //获取创建人
        String userId = request.getHeader("userId");
        //插入一条记录
        return incomeBasicInfoService.insert(incomeBasicInfoVo,userId);
    }

    /**
     *@Description: 逻辑删除一条记录
     *@Param: [request, id, deleteFlag]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/11/13
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, name = "逻辑删除一条记录")
    public ResponseTemplate delete(HttpServletRequest request,@RequestParam(name="id") String id, @RequestParam(name="deleteFlag") String deleteFlag) {
        //获取修改人
        String userId = request.getHeader("userId");
        //逻辑删除一条记录
        return incomeBasicInfoService.delete(id,userId,deleteFlag);
    }

    /**
     *@Description: 修改一条来件基础信息记录
     *@Param: [request, incomeBasicInfoVo]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/11/13
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, name = "修改一条来件基础信息记录")
    public ResponseTemplate update(HttpServletRequest request,@RequestBody IncomeBasicInfoVo incomeBasicInfoVo) {
        //获取修改人
        String userId = request.getHeader("userId");
        //修改一条记录
        return incomeBasicInfoService.update(incomeBasicInfoVo,userId);
    }

    /**
     *@Description: 来件基本信息确认
     *@Param: [workOrderNumber]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/11/18
     */
    @RequestMapping(value = "/confirmEntryInfo", method = RequestMethod.POST, name = "来件基本信息确认")
    public ResponseTemplate confirmEntryInfo(HttpServletRequest request,String workOrderNumber){
        String userId = request.getHeader("userId");
        return incomeBasicInfoService.confirmEntryInfo(workOrderNumber,userId);
    }
    /**
     *@Description: 保存图片
     *@Param: []
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/11/18
     */
    @RequestMapping(value = "/preview", method = RequestMethod.POST, name = "保存图片")
    public ResponseTemplate preview(@RequestParam("file") MultipartFile files) {
        //获取上传时的文件名称
        String fileName = files.getOriginalFilename();
        String path = System.getProperty("user.dir");
        String encode = null;
        try {
            BufferedOutputStream stream = null;
            path = System.getProperty("user.dir");
            byte[] bytes = files.getBytes();
            // 对字节数组Base64编码
            BASE64Encoder encoder = new BASE64Encoder();
            encode = encoder.encode(bytes);
            stream = new BufferedOutputStream(new FileOutputStream(new File(path+ File.separator + fileName)));
            stream.write(bytes);
            stream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String file = path + File.separator +fileName;
        DeleteFileUtil.delete(file);
        //String file = Base64Utils.GetImageStr(fileName);
        return incomeBasicInfoService.insertImg(encode);
    }


}
